using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Web.Security;
using PHSRAG.Security;
using System.Xml;
using System.IO;
using System.Text;
using AgreementConstants = PHSRAG.Insight21.Agreements.AgreementConstants;
using AgreementsConstants = PHSRAG.Insight21.InsightUtilities.Constants.AgreementsConstants;
using ExpenseConstants = PHSRAG.Insight21.Agreements.AgreementConstants.ExpenseInfoConstants;

namespace PHSRAG.Insight21.Agreements
{
    /// <summary>
    /// Summary description for StimulusFunding
    /// </summary>
    public class StimulusFundingReport
    {
        public XmlDocument doc = null;
        public StimulusFundingReport()
        {            
        }

        ///// <summary>
        ///// Method to generate the individual XML report for each AwardIdNumber.
        ///// </summary>
        ///// <param name="connectionString">Connection String</param>
        ///// <returns>Boolean</returns>
        //public bool CreateXmlReport(string connectionString, string strReportPeriod)
        //{
        //    System.Security.Principal.WindowsImpersonationContext wic = null;
        //    try
        //    {                
        //        bool returnBol = true;
        //        //bool blSubRecData = false;
        //        //bool blVendorData = false;
        //        string strAwardIdNumber = string.Empty;
        //        string strFundNumber = string.Empty;

        //        StimulusFunding sf = new StimulusFunding();

        //        // retrieving the report data for the 3 components
        //        DataTable dtProposal = sf.GetProposalReportData(connectionString, strReportPeriod); 
        //        DataTable dtSubRecipient = sf.GetSubRecipientReportData(connectionString, strReportPeriod);
        //        DataTable dtVendor = sf.GetVendorReportData(connectionString, strReportPeriod);

        //        // Impersonating the user
        //        wic = Authenticator.ImpersonateUser(
        //                       Utility.Settings.GetKeyValue("ArraServiceAccount", "RAG29"),
        //                       Utility.Settings.GetKeyValue("ArraSAPassword", "z0hcq"),
        //                       Authenticator.LogonType.NewCredentials, Authenticator.LogonProvider.WinNT50);


        //        // Loop through data for Prime Recipient            
        //        foreach (DataRow dr in dtProposal.Rows)
        //        {
        //            attachmentTitle = new XmlDocument();
        //            attachmentTitle.AppendChild(attachmentTitle.CreateProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"));

        //            // XML Header
        //            XmlNode root = attachmentTitle.CreateNode(XmlNodeType.Element, "recoveryrr-GrantLoanReport", null);
        //            XmlAttribute rootAttr = attachmentTitle.CreateAttribute("xsi:schemaLocation");
        //            rootAttr.Value = "urn:us:gov:recoveryrr recoveryrr-xc.xsd";
        //            root.Attributes.Append(rootAttr);

        //            rootAttr = attachmentTitle.CreateAttribute("xmlns:nc");
        //            rootAttr.Value = "http://niem.gov/niem/niem-core/2.0";
        //            root.Attributes.Append(rootAttr);

        //            rootAttr = attachmentTitle.CreateAttribute("xmlns:xsi");
        //            rootAttr.Value = "http://www.w3.org/2001/XMLSchema-instance";
        //            root.Attributes.Append(rootAttr);

        //            rootAttr = attachmentTitle.CreateAttribute("xmlns:recoveryrr");
        //            rootAttr.Value = "urn:us:gov:recoveryrr";
        //            root.Attributes.Append(rootAttr);

        //            rootAttr = attachmentTitle.CreateAttribute("xmlns:recoveryrr-ext");
        //            rootAttr.Value = "urn:us:gov:recoveryrr-ext";
        //            root.Attributes.Append(rootAttr);
                    
        //            // Grant Loan report Header
        //            XmlNode ReportHeader = addNode(attachmentTitle, root, "recoveryrr-ext-GrantLoanReportHeader", "");  // Addnode
        //            addNode(attachmentTitle, ReportHeader, "recoveryrr-ext-PrimeRecipientDUNS", dr["DunsNumber"].ToString());
        //            addNode(attachmentTitle, ReportHeader, "recoveryrr-ext-AwardIdNumber", dr["AwardNumber"].ToString());
        //            addNode(attachmentTitle, ReportHeader, "recoveryrr-ext-GrantLoanAwardCategory", dr["AwardType"].ToString());

        //            // Grant Loan Prime Recipient Report
        //            XmlNode PrimeRecipientReport = addNode(attachmentTitle, root, "recoveryrr-ext-GrantLoanPrimeRecipientReport", "");

        //            XmlNode LoanAwardDetail = addNode(attachmentTitle, PrimeRecipientReport, "recoveryrr-ext-GrantLoanAwardDetail", "");
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-FundingAgencyCode", dr["FundingAgencyCode"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-AwardingAgencyCode", dr["AwardingAgencyCode"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-FinalReportIndicator", (dr["FinalReport"].ToString().Trim() == "False"? "N":"Y"));

        //            XmlNode CompleteTreasuryAccountSymbol = addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-CompleteTreasuryAccountSymbol", "");
        //            addNode(attachmentTitle, CompleteTreasuryAccountSymbol, "recoveryrr-ext-TreasuryAccountSymbol", dr["ProgramSourceTAS"].ToString());
        //            addNode(attachmentTitle, CompleteTreasuryAccountSymbol, "recoveryrr-ext-SubAccountCode", dr["RecipientAccountNumber"].ToString());

        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-AwardDate", dr["AwardDate"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-AwardDescription", dr["AwardDescription"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-AwardAmount", dr["AwardedBudget"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-ProjectDescription", dr["QuarterlyActivities"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-ProjectStatus", dr["ProjectStatus"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-JobCreationNumber", dr["NumberOfJobs"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-JobCreationNarrative", dr["Narrative"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalFederalARRAReceived", dr["RevenueAmount"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalNumberSubawardsIndividuals", dr["TotalNumberOfSubAwards"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalAmountSubawardsIndividuals", dr["TotalSubAwardAmount"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalNumberSmallSubawards", dr["NumberOfSubAwardsLT25K"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalAmountSmallSubawards", dr["SubAwardsLT25KAmount"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-ProjectName", dr["ProgramTitle"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-CFDAProgramNumber", dr["CFDANumber"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-GrantLoanActivityCode", dr["ActivityCode"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalFederalARRAExpenditure", dr["TotalFederalARRAExpenditure"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalFederalARRAInfraExpenditure", "");
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-InfrastructureRationale", "");

        //            XmlNode InfrastructureContact = addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-InfrastructureContact", "");
        //            addNode(attachmentTitle, InfrastructureContact, "recoveryrr-ext-ContactName", "");

        //            XmlNode InfrastructureAddress = addNode(attachmentTitle, InfrastructureContact, "recoveryrr-ext-InfrastructureAddress", "");
        //            addNode(attachmentTitle, InfrastructureAddress, "nc-AddressDeliveryPointText", "");
        //            addNode(attachmentTitle, InfrastructureAddress, "nc-LocationCityName", "");
        //            addNode(attachmentTitle, InfrastructureAddress, "recoveryrr-ext-LocationState", "");
        //            addNode(attachmentTitle, InfrastructureAddress, "nc-LocationPostalCode", "");
        //            addNode(attachmentTitle, InfrastructureAddress, "nc-LocationPostalExtensionCode", "");

        //            addNode(attachmentTitle, InfrastructureContact, "nc-ContactEmailID", "");

        //            XmlNode FullTelephoneNumber = addNode(attachmentTitle, InfrastructureContact, "nc-FullTelephoneNumber", "");
        //            addNode(attachmentTitle, FullTelephoneNumber, "nc-TelephoneNumberFullID", "");
        //            addNode(attachmentTitle, FullTelephoneNumber, "nc-TelephoneSuffixID", "");

        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalNumberVendorPayments", dr["NoOfVendorPaymentLT25K"].ToString());
        //            addNode(attachmentTitle, LoanAwardDetail, "recoveryrr-ext-TotalAmountVendorPayments", dr["VendorPaymentsLT25K"].ToString());

        //            XmlNode PrimeRecipient = addNode(attachmentTitle, PrimeRecipientReport, "recoveryrr-ext-PrimeRecipient", "");
        //            addNode(attachmentTitle, PrimeRecipient, "recoveryrr-ext-CongressionalDistrict", dr["RecCongressionalDistrict"].ToString());
        //            addNode(attachmentTitle, PrimeRecipient, "recoveryrr-ext-AccountNumber", dr["RecipientAccountNumber"].ToString());
        //            addNode(attachmentTitle, PrimeRecipient, "recoveryrr-ext-CompensatedOfficerIndicator", "No");

        //            XmlNode CompensatedOfficer = addNode(attachmentTitle, PrimeRecipient, "recoveryrr-ext-CompensatedOfficer", "");
        //            addNode(attachmentTitle, CompensatedOfficer, "nc-PersonFullName", dr["PersonName"].ToString());
        //            addNode(attachmentTitle, CompensatedOfficer, "recoveryrr-ext-OfficerCompensation", dr["TotalCompensation"].ToString());

        //            XmlNode PlaceOfPerformance = addNode(attachmentTitle, PrimeRecipientReport, "recoveryrr-ext-PlaceOfPerformance", "");

        //            XmlNode Address = addNode(attachmentTitle, PlaceOfPerformance, "recoveryrr-ext-Address", "");
        //            addNode(attachmentTitle, Address, "nc-AddressDeliveryPointText", dr["StreetAddress1"].ToString() + " " + dr["StreetAddress2"].ToString());
        //            addNode(attachmentTitle, Address, "nc-LocationCityName", dr["City"].ToString());
        //            addNode(attachmentTitle, Address, "recoveryrr-ext-LocationState", dr["State"].ToString());
        //            addNode(attachmentTitle, Address, "recoveryrr-ext-LocationCountry", dr["Country"].ToString());
        //            addNode(attachmentTitle, Address, "nc-LocationPostalCode", dr["Zip"].ToString());
        //            addNode(attachmentTitle, Address, "nc-LocationPostalExtensionCode", "");

        //            addNode(attachmentTitle, PlaceOfPerformance, "recoveryrr-ext-CongressionalDistrict", dr["CongressionalDistrict"].ToString());

        //            // storing the Fund Number and AwardIdNumber.
        //            strFundNumber = dr["RecipientAccountNumber"].ToString();
        //            strAwardIdNumber = dr["AwardNumber"].ToString().ToLower().Trim();

        //            // Loop through data for Sub Recipient  
        //            //blSubRecData = false;
        //            foreach (DataRow dr1 in dtSubRecipient.Rows)
        //            {
        //                string subRecAwardNumber = dr1["AwardNumber"].ToString().ToLower().Trim();
        //                if (subRecAwardNumber == strAwardIdNumber)
        //                {
        //                    // Grant Loan Sub Recipient Report
        //                    XmlNode SubRecipientReport = addNode(attachmentTitle, root, "recoveryrr-ext-GrantLoanSubRecipientReport", "");
        //                    addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-SubAwardNumber", dr1["SubawardNumber"].ToString());
        //                    addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-AwardAmount", dr1["AmountOfSubaward"].ToString());
        //                    addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-AwardDate", dr1["SubawardDate"].ToString());

        //                    XmlNode SubRecipient = addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-SubRecipient", "");
        //                    addNode(attachmentTitle, SubRecipient, "recoveryrr-ext-SubRecipientDUNS", dr1["SubRecipientDUNSNumber"].ToString());
        //                    addNode(attachmentTitle, SubRecipient, "recoveryrr-ext-CongressionalDistrict", dr1["SubRecipientCongressionalDistrict"].ToString());
        //                    addNode(attachmentTitle, SubRecipient, "recoveryrr-ext-CompensatedOfficerIndicator", dr1["CompensatedOfficerIndicator"].ToString());

        //                    CompensatedOfficer = addNode(attachmentTitle, SubRecipient, "recoveryrr-ext-CompensatedOfficer", "");
        //                    addNode(attachmentTitle, CompensatedOfficer, "nc-PersonFullName", dr1["PersonName"].ToString());
        //                    addNode(attachmentTitle, CompensatedOfficer, "recoveryrr-ext-OfficerCompensation", dr1["TotalCompensation"].ToString());

        //                    PlaceOfPerformance = addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-PlaceOfPerformance", "");

        //                    Address = addNode(attachmentTitle, PlaceOfPerformance, "recoveryrr-ext-Address", "");
        //                    addNode(attachmentTitle, Address, "nc-AddressDeliveryPointText", dr1["StreetAddress1"].ToString() + " " + dr1["StreetAddress2"].ToString());
        //                    addNode(attachmentTitle, Address, "nc-LocationCityName", dr1["City"].ToString());
        //                    addNode(attachmentTitle, Address, "recoveryrr-ext-LocationState", dr1["State"].ToString());
        //                    addNode(attachmentTitle, Address, "recoveryrr-ext-LocationCountry", dr1["Country"].ToString());
        //                    addNode(attachmentTitle, Address, "nc-LocationPostalCode", dr1["Zip"].ToString());
        //                    addNode(attachmentTitle, Address, "nc-LocationPostalExtensionCode", "");

        //                    addNode(attachmentTitle, PlaceOfPerformance, "recoveryrr-ext-CongressionalDistrict", dr1["CongressionalDistrict"].ToString());
        //                    addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-SubAwardAmountDisbursed", dr1["TotalSubawardFundsDisbursed"].ToString());
        //                    //blSubRecData = true;
        //                }
        //            }

        //            // if there is no data for Sub recipient
        //            //if (blSubRecData == false)
        //            //{
        //            //    XmlNode SubRecipientReport = addNode(attachmentTitle, root, "recoveryrr-ext-GrantLoanSubRecipientReport", "");
        //            //    addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-SubAwardNumber", "");
        //            //    addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-AwardAmount", "");
        //            //    addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-AwardDate", "");

        //            //    XmlNode SubRecipient = addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-SubRecipient", "");
        //            //    addNode(attachmentTitle, SubRecipient, "recoveryrr-ext-SubRecipientDUNS", "");
        //            //    addNode(attachmentTitle, SubRecipient, "recoveryrr-ext-CongressionalDistrict", "");
        //            //    addNode(attachmentTitle, SubRecipient, "recoveryrr-ext-CompensatedOfficerIndicator", "");

        //            //    CompensatedOfficer = addNode(attachmentTitle, SubRecipient, "recoveryrr-ext-CompensatedOfficer", "");
        //            //    addNode(attachmentTitle, CompensatedOfficer, "nc-PersonFullName", "");
        //            //    addNode(attachmentTitle, CompensatedOfficer, "recoveryrr-ext-OfficerCompensation", "");

        //            //    PlaceOfPerformance = addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-PlaceOfPerformance", "");

        //            //    Address = addNode(attachmentTitle, PlaceOfPerformance, "recoveryrr-ext-Address", "");
        //            //    addNode(attachmentTitle, Address, "nc-AddressDeliveryPointText", "");
        //            //    addNode(attachmentTitle, Address, "nc-LocationCityName", "");
        //            //    addNode(attachmentTitle, Address, "recoveryrr-ext-LocationState", "");
        //            //    addNode(attachmentTitle, Address, "recoveryrr-ext-LocationCountry", "");
        //            //    addNode(attachmentTitle, Address, "nc-LocationPostalCode", "");
        //            //    addNode(attachmentTitle, Address, "nc-LocationPostalExtensionCode", "");

        //            //    addNode(attachmentTitle, PlaceOfPerformance, "recoveryrr-ext-CongressionalDistrict", "");
        //            //    addNode(attachmentTitle, SubRecipientReport, "recoveryrr-ext-SubAwardAmountDisbursed", "");
        //            //}

        //            // Loop through data for Vendor  
        //            //blVendorData = false;
        //            foreach (DataRow dr2 in dtVendor.Rows)
        //            {
        //                string vendorAwardNumber = dr2["AwardNumber"].ToString().ToLower().Trim();
        //                if (vendorAwardNumber == strAwardIdNumber)
        //                {
        //                    // Vendor Report
        //                    XmlNode VendorReport = addNode(attachmentTitle, root, "recoveryrr-ext-Vendor", "");
        //                    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-AwardIdNumber", dr2["AwardNumber"].ToString());
        //                    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-SubAwardNumber", dr2["SubawardNumber"].ToString());
        //                    addNode(attachmentTitle, VendorReport, "nc-OrganizationName", dr2["Name"].ToString());
        //                    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-VendorDUNS", dr2["DunsNumber"].ToString());
        //                    addNode(attachmentTitle, VendorReport, "nc-LocationPostalCode", dr2["HeadquartersZip"].ToString());
        //                    addNode(attachmentTitle, VendorReport, "nc-LocationPostalExtensionCode", "");
        //                    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-ProductServiceDescription", dr2["ItemDescription"].ToString());
        //                    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-PaymentAmount", dr2["PaymentAmount"].ToString());
        //                    //blVendorData = true;
        //                }
        //            }

        //            // if there is no data for Vendor.
        //            //if (blVendorData == false)
        //            //{
        //            //    XmlNode VendorReport = addNode(attachmentTitle, root, "recoveryrr-ext-Vendor", "");
        //            //    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-AwardIdNumber", "");
        //            //    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-SubAwardNumber", "");
        //            //    addNode(attachmentTitle, VendorReport, "nc-OrganizationName", "");
        //            //    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-VendorDUNS", "");
        //            //    addNode(attachmentTitle, VendorReport, "nc-LocationPostalCode", "");
        //            //    addNode(attachmentTitle, VendorReport, "nc-LocationPostalExtensionCode", "");
        //            //    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-ProductServiceDescription", "");
        //            //    addNode(attachmentTitle, VendorReport, "recoveryrr-ext-PaymentAmount", "");
        //            //}

        //            //  Replacing some xml nodes
        //            attachmentTitle.AppendChild(root);
        //            string sXml = attachmentTitle.InnerXml;
        //            sXml = sXml.Replace("schemaLocation", "xsi:schemaLocation");
        //            sXml = sXml.Replace("recoveryrr-ext-", "recoveryrr-ext:");
        //            sXml = sXml.Replace("recoveryrr-GrantLoanReport", "recoveryrr:GrantLoanReport");
        //            sXml = sXml.Replace("nc-", "nc:");


        //            // Save XML in the given path
        //            //string xmlPath = @"\\phsweb418\arra$\";
        //            string xmlPath = "C:\\StimulusProject\\ReportNew\\";
        //            string fileName = xmlPath + "Oct10" + strFundNumber + ".xml";
        //            if (strFundNumber == "213766")
        //                SaveXML(fileName, sXml);
        //        }

        //        // turn impersonation off
        //        Authenticator.RevertUser(wic);

        //        // Freeze the Report data for the current period for future updation.
        //        //sf.FreezeReportData(connectionString, strReportPeriod);

        //        //return the completion status
        //        return returnBol;
        //    }
        //    catch (Exception ex)
        //    {
        //        if(wic != null)
        //            Authenticator.RevertUser(wic);
        //        Utility.ExceptionReport.WriteToEventLog("Failed to generate xml reports ,Error occurred while executing the StimulusFunding.CreateXmlReport()", ex, true, Utility.ExceptionReport.ReportType.Error);
        //        throw;
        //    }
        //}

        ///// <summary>
        ///// Method to add a Xml node. 
        ///// </summary>
        ///// <param name="attachmentTitle">Xml Document</param>
        ///// <param name="root">Root Node</param>
        ///// <param name="name">Element Name</param>
        ///// <param name="val">Value</param>
        ///// <returns></returns>
        //private XmlNode addNode(XmlDocument attachmentTitle, XmlNode root, string name, string val)
        //{
        //    XmlNode node = attachmentTitle.CreateNode(XmlNodeType.Element, name, null);
        //    node.InnerText = val;
        //    root.AppendChild(node);
        //    return node;
        //}

        ///// <summary>
        ///// Method to add an Xml attribute to a node.
        ///// </summary>
        ///// <param name="attachmentTitle">Xml Document</param>
        ///// <param name="root">Root Node</param>
        ///// <param name="name">Element Name</param>
        ///// <param name="val">Value</param>
        //private void addAttribute(XmlDocument attachmentTitle, XmlNode root, string name, string val)
        //{
        //    XmlAttribute attribute = attachmentTitle.CreateAttribute(name);
        //    attribute.InnerText = val;
        //    root.Attributes.Append(attribute);
        //}

        ///// <summary>
        ///// Method to save the XML string to a XML file.
        ///// </summary>
        ///// <param name="FileName">XML file name</param>
        ///// <param name="sXml">XML string</param>
        //private void SaveXML(string FileName, string sXml)
        //{          
        //    StreamWriter objStreamWriter = File.CreateText(FileName);
        //    //File.Move(outputFile, stagedOutputFile);
        //    objStreamWriter.Write(sXml);
        //    objStreamWriter.Close();
        //}        
    }
}
